local extension = Package("ling5")
extension.extensionName = "lingling"

local U = require "packages/utility/utility"

local yangjian = General(extension, "lingling__yangjian", "sui", 5)
local daxing = fk.CreateViewAsSkill{
  name = "lingling__daxing",
  pattern = "duel,ex_nihilo",
  card_filter = function(self, to_select, selected)
    if #selected >= 2 then
      return false
    elseif #selected > 0 then
      return Fk:getCardById(to_select):compareColorWith(Fk:getCardById(selected[1]))
    else
      local color = Fk:getCardById(to_select).color
      local c
      if color == Card.Red then
        c = Fk:cloneCard("ex_nihilo")
      elseif color == Card.Black then
        c = Fk:cloneCard("duel")
      else
        return false
      end
      return (Fk.currentResponsePattern == nil and c.skill:canUse(Self, c))
      or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
    end
  end,
  view_as = function(self, cards)
    if #cards ~= 2 then
      return nil
    end
    local color = Fk:getCardById(cards[1]).color
    local c
    if color == Card.Red then
      c = Fk:cloneCard("ex_nihilo")
    elseif color == Card.Black then
      c = Fk:cloneCard("duel")
    else
      return nil
    end
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
}
local qingzha = fk.CreateTriggerSkill{
  name = "lingling__qingzha",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then 
    local room = player.room
    local move_event = room.logic:getCurrentEvent()
    local parent_event = move_event.parent
    local card_ids = {}
    self.cost_data ={{}}
    if parent_event ~= nil then
      if parent_event.event == GameEvent.UseCard then
        local parent_data = parent_event.data[1]
        if parent_data.from == player.id and parent_data.card.type == Card.TypeTrick then
          card_ids = room:getSubcardsByRule(parent_data.card)
        end
        if parent_data.damageDealt then
          local targets = TargetGroup:getRealTargets(parent_data.tos)
          for _, pid in ipairs(targets) do
            if parent_data.damageDealt[pid] and parent_data.damageDealt[pid]>0 then
              table.insertIfNeed(self.cost_data,"must")
              break
            end
          end
        end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        --[[if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
               table.insert(self.cost_data[1],info.cardId)
            end
          end
        else--]]if #card_ids > 0 then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) then
              table.insert(self.cost_data[1],info.cardId)
            end
          end
        end
      end
    end
    if #table.filter(self.cost_data[1], function(id) return room:getCardArea(id) == Card.DiscardPile end) >0 then
      self.cost_data[1] = table.filter(self.cost_data[1], function(id) return room:getCardArea(id) == Card.DiscardPile end)
    return true
    end
  end
end
end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if #self.cost_data>1 then
      return true
    else
      return room:askForSkillInvoke(player,self.name)
    end
  end,
  on_use = function(self, event, target, player, data)
   local room = player.room
   room:changeMaxHp(player, -1)
   room:moveCardTo(self.cost_data[1], Player.Hand,player, fk.ReasonPrey, self.name, nil, true, player.id)
  end,
}
yangjian:addSkill(daxing)
yangjian:addSkill(qingzha)
Fk:loadTranslationTable{
  ["lingling__yangjian"] = "杨坚",
  ["#lingling__yangjian"] = "晦明藏用",
  ["designer:lingling__yangjian"] = "伶",
  ["illustrator:lingling__yangjian"] = "珊瑚虫",

  ["lingling__daxing"] = "大兴",
  [":lingling__daxing"] = "你可以将两张红色牌当【无中生有】使用，或将两张黑色牌当【决斗】使用。",
  ["lingling__qingzha"] = "倾扎",
  [":lingling__qingzha"] = "每回合限一次，你使用的锦囊牌进入弃牌堆后，你可以减1点体力上限获得之，若此牌对目标造成了伤害，你必须如此做。"..
  "<br><br> <font color='#a40000'>普六茹坚相貌非常，人颇狡诈。</font>",
}

return extension
